<template>
  <a-modal
    :title='title'
    :width='800'
    :visible='visible'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>

    <a-spin :spinning='confirmLoading'>
      <a-form :form='form'>
        <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='所属项目'>
          <a-select v-decorator="['projectId', {rules:[{required:true,message:'请选择所属项目'}]}]">
            <a-select-option v-for='project in this.projectSelectOption' :key='project.id' :value='project.id'>
              {{ project.name }}
            </a-select-option>
          </a-select>
        </a-form-item>


        <a-form-item
          :labelCol='labelCol'
          :wrapperCol='wrapperCol'
          label='类型名称'>
          <a-auto-complete
            :data-source='typeList'
            placeholder='请选择类型'
            :filter-option='filterOption'
            v-decorator="['typeName', {rules:[{required:true,message:'请输入类型'},{validator: this.checkType}]}]"
          />
        </a-form-item>
        <a-form-item
          :labelCol='labelCol'
          :wrapperCol='wrapperCol'
          label='类型别名'>
          <a-input placeholder='请输入类型别名' v-decorator="['typeAlias', {}]" />
        </a-form-item>
        <a-form-item
          v-show='false'
          :labelCol='labelCol'
          :wrapperCol='wrapperCol'
          label='创建人'>
          <a-input placeholder='请输入创建人' v-decorator="['createUser', {}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'

export default {
  name: 'ProjectUserTypeModal',
  props: ['projectSelectOption'],
  data() {
    return {
      title: '操作',
      visible: false,
      model: { typeName: '' },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        projectId: { rules: [{ required: true, message: '请选择所属项目!' }] }
      },
      url: {
        add: '/projectUserType/projectUserType/add',
        edit: '/projectUserType/projectUserType/edit'
      },
      projectList: [],
      typeList: ['户主', '家人', '亲友', '租客', '办公人员', '公司职员', '物业员工']
    }
  },
  created() {
  },
  methods: {
    checkTypeNum(rule,value,callback){
      if (value==='1'){
        callback(new Error('1已被占用'))
      }
      callback()
    },
    checkType(rule, value, callback) {
      let projectId = this.form.getFieldValue('projectId')
      /*查找项目下是否存在该类型名*/
      if (value === undefined) {
        callback(new Error('请输入类型名称'))
      }
      if (value === '户主') {
        callback(new Error('不允许添加户主'))
      }
      callback()
    },
    projectHandleSelectChange(value, option) {

    },
    typeListSelectChange(value, option) {

    },
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'projectId', 'typeName', 'typeAlias'))
      })

    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      debugger
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })

        }else{
          that.$message.warn(err)
          that.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    filterOption(input, option) {

      input = String(input)
      return (
        option.componentOptions.children[0].text.toUpperCase().indexOf(input.toUpperCase()) >= 0
      )
    }
  }
}
</script>

<style lang='less' scoped>

</style>